package com.example.poemtest.mine;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.poemtest.Activity.BottomNavigationViewActivity;
import com.example.poemtest.MineFragment;
import com.example.poemtest.R;
import com.example.poemtest.entity.PropInfoBean;
import com.example.poemtest.utils.Constant;
import com.example.poemtest.utils.FullScreenUtil;
import com.example.poemtest.utils.HttpUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 我的道具页面
 */
public class MyPropActivity extends AppCompatActivity {

    private ImageView next;
    private GridView gridView;
    private List<MyProp> myPropList;
    private MyPropAdapter myPropAdapter;
    private LinearLayout propShop;
    private int code;   //返回的状态码
    private String msgCon;  //返回的错误信息
    private Handler myHandler = new Handler(Looper.myLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            //处理子线程发送的消息
            String result = (String) msg.obj;
            try {
                JSONObject jsonObject = new JSONObject(result);
                code = jsonObject.getInt("code");
                msgCon = jsonObject.getString("msg");
                Log.e("状态码",code+"");
                Log.e("返回的提示信息：",msgCon);
                if (code == 200){
                    //解析json文件流
                    PropInfoBean propInfoBean = com.alibaba.fastjson.JSONObject.parseObject(result,PropInfoBean.class);
                    List<PropInfoBean.PropInfo> list = propInfoBean.getData();
                    for (PropInfoBean.PropInfo propInfo : list) {
                        Constant.propInfoList.add(propInfo);
                    }
                    for (PropInfoBean.PropInfo propInfo : Constant.propInfoList) {
                        Log.e("收到的道具数据",propInfo.toString());
                    }
                } else {
                    Toast.makeText(MyPropActivity.this,msgCon,Toast.LENGTH_LONG).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FullScreenUtil.getInstance().fullScreen(MyPropActivity.this,true);
        setContentView(R.layout.activity_my_prop);
        //绑定控件对象
        initView();
        //设置数据源
//        setData();
        //为下一页的道具设置数据源
        if (Constant.propInfoList.size() == 0) {
            getCoon();
        }
//        getCoon1();
        //初始化我的道具适配器
        myPropAdapter = new MyPropAdapter(
                this,
                Constant.myPropList,
                R.layout.layout_my_prop_item
        );
        gridView.setAdapter(myPropAdapter);
        //点击道具商城
        propShop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到诗词道具商城的页面
                Intent intent = new Intent(MyPropActivity.this,PoetryPropActivity.class);
                startActivity(intent);
            }
        });
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyPropActivity.this, BottomNavigationViewActivity.class);
                intent.putExtra("id",4);
                startActivity(intent);
            }
        });

    }

    /**
     * 设置数据源
     */
    private void setData() {
        myPropList = new ArrayList<>();
        MyProp myProp = new MyProp();
        myProp.setMyPropImg("http://hn-bkt.clouddn.com/img/prop.jpg");
        myProp.setMyPropName("红宝石");
        myProp.setMyPropNum("2");
        while (myPropList.size() < 10) {
            myPropList.add(myProp);
        }
//        myPropList.add(myProp);
//        myPropList.add(myProp);
//        myPropList.add(myProp);
//        myPropList.add(myProp);
//        myPropList.add(myProp);
    }

    /**
     * 绑定控件对象
     */
    private void initView() {
        next = findViewById(R.id.setup_safe_to_mine);
        gridView = findViewById(R.id.my_prop_grid_view);
        propShop = findViewById(R.id.prop_shop);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //初始化我的道具适配器
        myPropAdapter = new MyPropAdapter(
                this,
                Constant.myPropList,
                R.layout.layout_my_prop_item
        );
        gridView.setAdapter(myPropAdapter);
    }


    /**
     * 获取网络连接
     */
    private void getCoon(){
        //创建okhtto请求的对象
        OkHttpClient okHttpClient = new OkHttpClient();
        //创建请求连接，url里面存放请求连接，get表示其实get请求
//        Request request = new Request.Builder()
//                .url("http://192.168.43.131:8080/userInfo/login")
//                .post(requestBody)
//                .build();
        Request request = new Request.Builder()
                .url("http://192.168.43.131:8080/propInfo/findPorpAll")
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("okhttp","请求失败");
                e.printStackTrace();
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //处理后端传递的数据
                Log.e("okhttp","请求成功");
                String responseDate = response.body().string();
                Log.e("返回的数据",responseDate);
                //数据源发送改变，需要更新界面，使用Handler
                Message message = new Message();
                message.obj = responseDate;
                message.arg1 = 0;
                myHandler.sendMessage(message);//发送到主线程的消息队列
            }
        });
    }

}